package org.ehe.business.bom.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.bom.domain.BomStructureTemp;
import org.ehe.business.bom.domain.bo.BomStructureTempBo;
import org.ehe.business.bom.domain.vo.BomStructureTempVo;
import org.ehe.business.bom.mapper.BomStructureTempMapper;
import org.ehe.business.bom.service.BomStructureTempService;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.core.utils.object.BeanUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.satoken.utils.LoginHelper;
import org.ehe.erp.domain.product.ErpMaterial;
import org.ehe.erp.domain.product.vo.ErpProductCategoryVo;
import org.ehe.erp.domain.product.vo.ErpProductUnitVo;
import org.ehe.erp.domain.product.vo.ErpProductVo;
import org.ehe.erp.service.product.IErpProductCategoryService;
import org.ehe.erp.service.product.IErpProductService;
import org.ehe.erp.service.product.IErpProductUnitService;
import org.ehe.erp.service.product.MaterialService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author :zhangnn
 * @className :BomStructureServiceImpl
 * @description: TODO
 * @date 2025-07-30 14:01:31
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BomStructureServiceTempImpl extends ServiceImpl<BomStructureTempMapper, BomStructureTemp>
        implements BomStructureTempService {

    private final BomStructureTempMapper tempMapper;
    private final IErpProductCategoryService categoryService;
    private final IErpProductService productService;
    private final IErpProductUnitService erpProductUnitService;
    private final MaterialService materialService;

    /**
     * 分页
     * 
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<BomStructureTempVo> queryList(BomStructureTempBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BomStructureTemp> lqw = new LambdaQueryWrapper<>();
        lqw.eq(BomStructureTemp::getImportId, bo.getBomVersionId());
        IPage<BomStructureTempVo> bomStructureTempVoIPage = tempMapper.selectVoPage(pageQuery.build(), lqw);

        bomStructureTempVoIPage.getRecords().forEach(p -> {
            ErpProductVo erpProductVo = productService.queryById(p.getItemId());
            ErpProductVo p2 = productService.buildProductVO(erpProductVo);
            p.setCategoryCode(erpProductVo.getCategoryCode());
            p.setFullCode(p2.getFullCode());
            p.setFullName(p2.getFullName());
            p.setUnitName(p2.getUnitName());
        });

        return TableDataInfo.build(bomStructureTempVoIPage);
    }

    @Override
    public Integer addStructure(BomStructureTemp dto) {
        dto.setTenantId(LoginHelper.getTenantId());
        return this.baseMapper.insert(dto);
    }

    @Override
    public Integer editStructure(BomStructureTemp dto) {
        return this.baseMapper.updateById(dto);
    }

    @Override
    public Integer deleteStructure(Long id) {
        return this.baseMapper.deleteById(id);
    }
}
